﻿package cn.poco.pepsi.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import android.content.Context;
import android.util.Log;
import cn.poco.pepsi.bean.City;
import cn.poco.pepsi.service.ICity;
import cn.poco.pepsi.util.DownloadImage;
import cn.poco.pepsi.util.UrlConnectionUtil;
import cn.poco.pepsi.util.UrlMatchUtil;
import cn.poco.pepsi.util.xmlpared.CityXmlparse;
import cn.poco.pepsi.util.xmlpared.GPSCityXmlparse;


/**
 * 此类可以用于获取全部城市列表和根据具体的经纬度获取城市
 */
public class CityImpl implements ICity {

	@Override
	public List<City> switchCity(Context context) throws Exception {
		List<City> citys;
		 String path =UrlMatchUtil.matchUrl(CITY_URL);
		 Log.i("impl", path);
		String url1=new DownloadImage(context).download(path);
		InputStream inSteam = new FileInputStream(new File(url1));
		citys = CityXmlparse.getXml(inSteam);
		return citys;
	}

	@Override
	public City switchCitybyGps(double lat, double lon) throws Exception {
		String url = CITY_GPS_URL.replace("#a", String.valueOf(lat));
		url = url.replace("#b", String.valueOf(lon));
		url=UrlMatchUtil.matchUrl(url);
		Log.i(TAG, url);
		InputStream inSteam = UrlConnectionUtil.getRequest(url);
		//City city = GPSCityXmlparse.getXml(inSteam);
		//测试用
		City city=new City();
		city.setKey("101029001");
		city.setName("广州");
		return city;
	}

}
